const app = getApp()
const utils = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    skin: '',
    no: '',  // 设备号
    barnName: '',
    zoneName: '',
    devName : '',
    areaId : 0,
    page : 1,
    haveNext: false,
    dataList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      areaId : options.areaId || '',
      no : options.dceNo || '',
      deviceId : options.deviceId || 0,
      barnName: options.barnName || '',
      zoneName : options.zoneName || '',
      devName : options.devName || '',
      page : 1
    });
    this.loadList(1);

    app.setNavBar()
    const { skin } = app.globalData
    this.setData({
      skin
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.haveNext) {
      this.setData({
        page: this.data.page + 1
      });
      this.loadList(this.data.page);
    }
  },
  loadList: function (page) {
    let that = this;
    let getListParams = {
      "areaId": that.data.areaId,
      "page": page,
      "pageSize": 15,
    };
    utils.promiseGet('/wx/device/spray/getAreaTempDataPage', getListParams).then(res => {
      let list = that.data.dataList;
      let haveNext = that.data.haveNext;
      if (page == 1) {
        list = [];
      }
      if (res.code == 200) {
        let result = res.data;
        if (result.rows.length > 0) {
          for (let i = 0; i < result.rows.length; i++) {
            let itemRow = {
              "time" : result.rows[i].createTime,
              "shidu" : result.rows[i].humidity,
              "wendu" : result.rows[i].temperature
            };
            list.push(itemRow);
          }
          haveNext = true;
        } else {
          haveNext = false;
        }
      } else {
      }
      this.setData({
        dataList: list,
        haveNext: haveNext
      });
    }).catch(e => {
      console.log(e);
    });
  }
})